package com.gt.xmall.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.util.AntPathMatcher;

import com.gt.xmall.Constants;
//import org.springframework.security.web.util.AntUrlPathMatcher;
//import org.springframework.security.web.util.UrlMatcher;





public class DBInvocationSecurityMetadataSource implements
		FilterInvocationSecurityMetadataSource {
	private static final Logger log = LoggerFactory.getLogger(DBInvocationSecurityMetadataSource.class);
//	private static Map<String, Collection<ConfigAttribute>> resourceMap = null;
	public DBInvocationSecurityMetadataSource(){
		loadResourceDefine();
	}
	
	
	private void loadResourceDefine() {
		// TODO Auto-generated method stub
//		resourceMap  = Constants.resourceMap;
//        Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
//        ConfigAttribute ca = new SecurityConfig("ROLE_ADMIN");
//        atts.add(ca);
//        resourceMap.put("/index.jsp", atts);
//        resourceMap.put("/i.jsp", atts);
	}

	private AntPathMatcher urlMatcher = new  AntPathMatcher();
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<ConfigAttribute> getAttributes(Object object)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		String url = ((FilterInvocation)object).getRequestUrl();
//		log.info("rurl="+url);
		
//		log.info("getHttpRequest="+((FilterInvocation)object).getRequest().getParameter("strAction"));
//		log.info("getHttpRequest2="+(((FilterInvocation)object).getHttpRequest()).getParameter("strAction"));
		try {
			String strAction=((FilterInvocation)object).getHttpRequest().getParameter("strAction");
			if (StringUtils.isNotEmpty(url) && url.indexOf('?') > -1) {
				url = url.substring(0, url.indexOf('?'));
			}	
			if(StringUtils.isNotEmpty(strAction)){
				url +="?strAction="+strAction+"&1=1";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("can not get parameter..."+e.getMessage());
		}
		
//		log.info("url="+url);
	        Iterator<String> ite = Constants.resourceMap.keySet().iterator();
	        while (ite.hasNext()) {
	            String resURL = ite.next();
	            if (urlMatcher.match(resURL, url)) {
//	            	log.info("match="+resURL);
	                return (Collection<ConfigAttribute>) Constants.resourceMap.get(resURL);
	            }
	        }
	        return null;
	
	}

	public boolean supports(Class<?> clazz) {
		// TODO Auto-generated method stub
		return true;
	}

}
